home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / cmdline.lha / cmdline / src / cmd / fsm.h < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-03  |  2.2 KB  |  79 lines

  1. //------------------------------------------------------------------------
  2. // ^FILE: fsm.h - define a finite state machine
  3. //
  4. // ^DESCRIPTION:
  5. //     This file defines a finite state machine tailored to the task of
  6. //     parsing syntax strings for command-line arguments.
  7. //
  8. // ^HISTORY:
  9. //    03/27/92    Brad Appleton    <brad@ssd.csd.harris.com>    Created
  10. //-^^---------------------------------------------------------------------
  11.  
  12. class SyntaxFSM {
  13. public:
  14.    enum state_t {
  15.       START,    // start-state
  16.       OPTION,   // just parsed an option-spec
  17.       KEYWORD,  // just parsed a keyword-spec
  18.       VALUE,    // just parsed a value-spec
  19.       LIST,     // just parsed "..."
  20.       FINAL,    // final-state
  21.       ERROR,    // syntax error encountered
  22.    } ;
  23.  
  24.    struct token_t {
  25.       const char * start;  // start address of token
  26.       unsigned     len;    // length of token
  27.  
  28.       token_t(void) : start(0), len(0) {}
  29.  
  30.       void
  31.       set(const char * s, unsigned l) { start = s, len = l; }
  32.    } ;
  33.  
  34.    SyntaxFSM(void) : ntoks(0), nbpairs(0), lev(0), fsm_state(START) {}
  35.  
  36.       // Reset the FSM 
  37.    void
  38.    reset(void) { ntoks = 0; nbpairs = 0; lev = 0; fsm_state = START; }
  39.  
  40.       // Return the number of tokens parsed thus far.
  41.    unsigned
  42.    num_tokens(void) const  { return  ntoks; }
  43.  
  44.       // Return the number of balanced brace-pairs parsed thus far.
  45.    unsigned
  46.    num_braces(void) const  { return  nbpairs; }
  47.  
  48.       // Return the current nesting level of brace-pairs
  49.    int
  50.    level(void) const  { return  lev; }
  51.  
  52.       // Return the current machine state
  53.    state_t
  54.    state(void) const  { return  fsm_state; }
  55.  
  56.       // Get the next token from "input" and place it in "token"
  57.       // (consuming characters from "input").
  58.       //
  59.       // Return 0 if the resulting state is FINAL;
  60.       // otherwise return NON-zero.
  61.       //
  62.    int
  63.    operator()(const char * & input, token_t & token);
  64.  
  65. protected:
  66.    unsigned  ntoks;      // number of tokens parsed thus far
  67.    unsigned  nbpairs;    // number of balanced brace-pairs parsed thus far
  68.    int       lev;        // current nesting level of brace-pairs
  69.    state_t   fsm_state;  // current machine state
  70.  
  71. private:
  72.    void
  73.    skip(const char * & input);
  74.  
  75.    void
  76.    parse_token(const char * & input);
  77. } ;
  78.  
  79.